package com.jl.jlapp.mvp.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jl.jlapp.R;
import com.jl.jlapp.adapter.AfterSaleApplyGoodsListAdapter;
import com.jl.jlapp.eneity.ApplyCustomerServiceModel;
import com.jl.jlapp.eneity.ImageBean;
import com.jl.jlapp.eneity.ImgUploadModel;
import com.jl.jlapp.eneity.OrderDetail;
import com.jl.jlapp.nets.CommonObserver;
import com.jl.jlapp.nets.Net;
import com.jl.jlapp.popu.ClearHistorySearhPopu;
import com.jl.jlapp.utils.CustomLanearLayoutManager;
import com.jl.jlapp.utils.MimeTypeUtils;
import com.jl.jlapp.utils.Tools;
import com.jl.jlapp.utils.loader.Loader;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;
import com.yzs.imageshowpickerview.ImageShowPickerBean;
import com.yzs.imageshowpickerview.ImageShowPickerListener;
import com.yzs.imageshowpickerview.ImageShowPickerView;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.PicassoEngine;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.MultipartBody.Part;
import okhttp3.RequestBody;

public class AfterSaleApplyActivity
        extends AppCompatActivity
        implements OnClickListener {
    private static final int REQUEST_CODE_CHOOSE = 233;
    private static final String TAG = "AfterSaleApplyActivity";
    @BindView(R.id.btn_return)
    ImageView btn_return;
    @BindView(R.id.change_goods)
    TextView change_goods;
    ClearHistorySearhPopu clearHistorySearhPopu;
    @BindView(R.id.commit_view)
    TextView commit_view;
    public OnClickListener confirmClick = new OnClickListener() {
        public void onClick(View paramAnonymousView) {
            AfterSaleApplyActivity.this.clearHistorySearhPopu.dismiss();
            Intent intent = new Intent(AfterSaleApplyActivity.this, OrderDetailActivity.class);
            intent.putExtra("netCode", AfterSaleApplyActivity.this.netCode);
            AfterSaleApplyActivity.this.setResult(0, intent);
            Tools.finishAll();
        }
    };
    String describeStr = "";
    @BindView(R.id.describe)
    EditText describeView;
    AfterSaleApplyGoodsListAdapter goodsListAdapter;
    String intentName = "";
    String intentPhone = "";
    @BindView(R.id.length_limit)
    TextView lengthLimitTextView;
    List<ImageBean> list;
    @BindView(R.id.name)
    EditText name;
    String nameStr = "";
    int netCode = -1;
    ArrayList<OrderDetail.ResultDataBean.OrderDetailsBean> orderDetailsBeanList = new ArrayList();
    int orderId = -1;
    @BindView(R.id.other)
    TextView other;
    MultipartBody.Part[] partList = new MultipartBody.Part[10];
    String phoneNumberStr = "";
    @BindView(R.id.phone_number)
    EditText phone_number;
    String[] picUrlArr = new String[10];
    List<String> picUrlList = new ArrayList();
    @BindView(R.id.it_picker_view)
    ImageShowPickerView pickerView;
    private ProgressDialog progressDialog;
    private RationaleListener rationaleListener = new RationaleListener() {
        public void showRequestPermissionRationale(int paramAnonymousInt, Rationale paramAnonymousRationale) {
            paramAnonymousRationale.resume();
        }
    };
    List<String> realPicUrlList = new ArrayList();
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.return_goods)
    TextView return_goods;
    int serviceType = -1;
    int userId = -1;

    private void backToDefaultStyle() {
        this.serviceType = -1;
        this.return_goods.setBackgroundResource(R.drawable.btn_aftermarket_gray);
        this.return_goods.setTextColor(getResources().getColor(R.color.font_grey));
        this.change_goods.setBackgroundResource(R.drawable.btn_aftermarket_gray);
        this.change_goods.setTextColor(getResources().getColor(R.color.font_grey));
        this.other.setBackgroundResource(R.drawable.btn_aftermarket_gray);
        this.other.setTextColor(getResources().getColor(R.color.font_grey));
    }

    @PermissionNo(300)
    private void getPermissionNo(List<String> paramList) {
        Toast.makeText(this, "未授权", Toast.LENGTH_SHORT).show();
    }

    @PermissionYes(300)
    private void getPermissionYes(List<String> paramList) {
    }

    private void init() {
        this.phone_number.setText(this.intentPhone);
        this.name.setText(this.intentName);
        this.phone_number.setInputType(3);
        setListener();
        getNetData();
        this.btn_return.setOnClickListener(new OnClickListener() {
            public void onClick(View paramAnonymousView) {
                if (AfterSaleApplyActivity.this.clearHistorySearhPopu != null) {
                    AfterSaleApplyActivity.this.clearHistorySearhPopu.dismiss();
                    Intent intent = new Intent(AfterSaleApplyActivity.this, OrderDetailActivity.class);
                    intent.putExtra("netCode", AfterSaleApplyActivity.this.netCode);
                    AfterSaleApplyActivity.this.setResult(0, intent);
                    Tools.finishAll();
                    return;
                }
                Intent intent = new Intent(AfterSaleApplyActivity.this, OrderDetailActivity.class);
                intent.putExtra("netCode", AfterSaleApplyActivity.this.netCode);
                AfterSaleApplyActivity.this.setResult(0, intent);
                AfterSaleApplyActivity.this.finish();
            }
        });
        this.commit_view.setOnClickListener(new OnClickListener() {
            public void onClick(View paramAnonymousView) {
                Tools.addActivity(AfterSaleApplyActivity.this);
                AfterSaleApplyActivity.this.describeStr = AfterSaleApplyActivity.this.describeView.getText().toString().trim();
                AfterSaleApplyActivity.this.phoneNumberStr = AfterSaleApplyActivity.this.phone_number.getText().toString().trim();
                AfterSaleApplyActivity.this.nameStr = AfterSaleApplyActivity.this.name.getText().toString().toString().trim();
                if ((AfterSaleApplyActivity.this.orderId == -1) || (AfterSaleApplyActivity.this.userId == -1)) {
                    Toast.makeText(AfterSaleApplyActivity.this, "内部错误", Toast.LENGTH_SHORT).show();
                }

                if (AfterSaleApplyActivity.this.serviceType == -1) {
                    Toast.makeText(AfterSaleApplyActivity.this, "请选择服务类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(AfterSaleApplyActivity.this.describeStr)) {
                    Toast.makeText(AfterSaleApplyActivity.this, "请填写问题描述", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(AfterSaleApplyActivity.this.nameStr)) {
                    Toast.makeText(AfterSaleApplyActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(AfterSaleApplyActivity.this.phoneNumberStr)) {
                    Toast.makeText(AfterSaleApplyActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!AfterSaleApplyActivity.this.isMobile(AfterSaleApplyActivity.this.phoneNumberStr)) {
                    Toast.makeText(AfterSaleApplyActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }

//                if ((AfterSaleApplyActivity.this.serviceType == -1) || ("".equals(AfterSaleApplyActivity.this.describeStr)) || ("".equals(AfterSaleApplyActivity.this.nameStr)) || ("".equals(AfterSaleApplyActivity.this.phoneNumberStr)) || (!AfterSaleApplyActivity.this.isMobile(AfterSaleApplyActivity.this.phoneNumberStr))){
//                }


                AfterSaleApplyActivity.this.buildProgressDialog();
                if (AfterSaleApplyActivity.this.realPicUrlList.size() > 0) {
                    AfterSaleApplyActivity.this.photo(AfterSaleApplyActivity.this.realPicUrlList);
                    AfterSaleApplyActivity.this.commit_view.setClickable(false);
                    AfterSaleApplyActivity.this.commit_view.setTextColor(AfterSaleApplyActivity.this.getResources().getColor(R.color.font_grey));
                    Net.get().uploadPhoto(AfterSaleApplyActivity.this.partList).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new CommonObserver<ImgUploadModel>() {
                                @Override
                                protected void onError(String errorMsg) {

                                }

                                @Override
                                protected void onSuccess(ImgUploadModel imgUploadModel) {

                                }
                            });
                    return;
                }
                Net.get().applyCustomerService(AfterSaleApplyActivity.this.serviceType, AfterSaleApplyActivity.this.userId, AfterSaleApplyActivity.this.orderId, AfterSaleApplyActivity.this.describeStr, AfterSaleApplyActivity.this.nameStr, AfterSaleApplyActivity.this.phoneNumberStr, AfterSaleApplyActivity.this.picUrlList)
                        .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new CommonObserver<ApplyCustomerServiceModel>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(ApplyCustomerServiceModel applyCustomerServiceModel) {

                    }
                });
            }
        });
        this.describeView.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable paramAnonymousEditable) {
            }

            public void beforeTextChanged(CharSequence paramAnonymousCharSequence, int paramAnonymousInt1, int paramAnonymousInt2, int paramAnonymousInt3) {
            }

            public void onTextChanged(CharSequence paramAnonymousCharSequence, int paramAnonymousInt1, int paramAnonymousInt2, int paramAnonymousInt3) {
                AfterSaleApplyActivity.this.handleLengthLimit();
            }
        });
    }

    private void photo(List<String> paramList) {
        this.partList = new MultipartBody.Part[10];
        int i = 0;
        while (i < paramList.size()) {
            File localObject = new File((String) paramList.get(i));
            if (localObject != null) {
                RequestBody localRequestBody = RequestBody.create(MediaType.parse(MimeTypeUtils.getMimeType((File) localObject)), (File) localObject);
                Part part = MultipartBody.Part.createFormData("file", ((File) localObject).getName(), localRequestBody);
                this.partList[i] = part;
            }
            i += 1;
        }
    }

    private void setListener() {
        this.return_goods.setOnClickListener(this);
        this.change_goods.setOnClickListener(this);
        this.other.setOnClickListener(this);
    }

    public void buildProgressDialog() {
        if (this.progressDialog == null) {
            this.progressDialog = new ProgressDialog(this);
            this.progressDialog.setProgressStyle(0);
        }
        this.progressDialog.setMessage("加载中");
        this.progressDialog.setCancelable(true);
        this.progressDialog.show();
    }

    public void cancelProgressDialog() {
        if ((this.progressDialog != null) && (this.progressDialog.isShowing())) {
            this.progressDialog.dismiss();
        }
    }

    public void getNetData() {
        if ((this.orderId != -1) && (this.userId != -1)) {
            Net.get().getOrderDetail(this.orderId, this.userId).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new CommonObserver<OrderDetail>() {
                        @Override
                        protected void onError(String errorMsg) {

                        }

                        @Override
                        protected void onSuccess(OrderDetail orderDetail) {

                        }
                    });
        }
        if (this.userId == -1) {
            startActivity(new Intent(this, LoginActivity.class));
            return;
        }

        if (this.orderId != -1)
            Toast.makeText(this, "页面传值没有接收到", Toast.LENGTH_SHORT).show();
    }

    public String getRealFilePath(Context paramContext, Uri paramUri) {
        if (paramUri == null) {
            return null;
        }
        return "";
//        String str = paramUri.getScheme();
//        Object localObject2 = null;
//        Object localObject3 = null;
//        Object localObject1;
//        if (str == null) {
//            localObject1 = paramUri.getPath();
//        }
//        for (; ; ) {
//            this.realPicUrlList.add(localObject1);
//            return (String) localObject1;
//            if ("file".equals(str)) {
//                localObject1 = paramUri.getPath();
//            } else {
//                localObject1 = localObject3;
//                if ("content".equals(str)) {
//                    str = paramUri.getAuthority();
//                    if (str.equals("media")) {
//                        paramContext = paramContext.getContentResolver().query(paramUri, new String[]{"_data"}, null, null, null);
//                        localObject1 = localObject3;
//                        if (paramContext != null) {
//                            localObject1 = localObject2;
//                            if (paramContext.moveToFirst()) {
//                                int i = paramContext.getColumnIndex("_data");
//                                localObject1 = localObject2;
//                                if (i > -1) {
//                                    localObject1 = paramContext.getString(i);
//                                }
//                            }
//                            paramContext.close();
//                        }
//                    } else {
//                        localObject1 = localObject3;
//                        if (str.equals("com.jl.jlapp.publish.MyFileProvider")) {
//                            localObject1 = Environment.getExternalStorageDirectory() + "/Pictures/" + paramUri.getLastPathSegment();
//                        }
//                    }
//                }
//            }
//        }
    }

    public void handleImgPicker() {
        this.list = new ArrayList();
        this.pickerView.setImageLoaderInterface(new Loader());
        this.pickerView.setNewData(this.list);
        this.pickerView.setShowAnim(true);
        this.pickerView.setMaxNum(5);
        this.pickerView.setOneLineShowNum(5);
        this.pickerView.setmDelLabel(2131165417);
        this.pickerView.setmAddLabel(2131165621);
        this.pickerView.setmPicSize(200);
        this.pickerView.setPickerListener(new ImageShowPickerListener() {
            public void addOnClickListener(int paramAnonymousInt) {
                if ((ActivityCompat.checkSelfPermission(AfterSaleApplyActivity.this, "android.permission.READ_EXTERNAL_STORAGE") != 0) || (ActivityCompat.checkSelfPermission(AfterSaleApplyActivity.this, "android.permission.READ_EXTERNAL_STORAGE") != 0)) {
                    AndPermission.with(AfterSaleApplyActivity.this).requestCode(300).permission(new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE"}).rationale(AfterSaleApplyActivity.this.rationaleListener).callback(this).start();
                    return;
                }
                Matisse.from(AfterSaleApplyActivity.this).choose(MimeType.of(MimeType.JPEG, new MimeType[]{MimeType.PNG})).countable(true).maxSelectable(paramAnonymousInt + 1).capture(true).captureStrategy(new CaptureStrategy(true, "com.jl.jlapp.publish.MyFileProvider")).restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED).gridExpectedSize(300).thumbnailScale(0.85F).imageEngine(new PicassoEngine()).theme(R.style.Matisse_Dracula).forResult(233);
            }

            public void delOnClickListener(int paramAnonymousInt1, int paramAnonymousInt2) {
                AfterSaleApplyActivity.this.realPicUrlList.remove(paramAnonymousInt1);
            }

            public void picOnClickListener(List<ImageShowPickerBean> paramAnonymousList, int paramAnonymousInt1, int paramAnonymousInt2) {
                Intent intent = new Intent(AfterSaleApplyActivity.this, PhotoViewActivity.class);
                intent.putExtra("picUrl", (String) AfterSaleApplyActivity.this.realPicUrlList.get(paramAnonymousInt1));
                intent.putExtra("flag", 1);
                AfterSaleApplyActivity.this.startActivity(intent);
            }
        });
        this.pickerView.show();
        AndPermission.with(this).requestCode(300).permission(new String[]{"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.CAMERA"}).rationale(this.rationaleListener).callback(this).start();
        this.pickerView.getDataList();
    }

    public void handleLengthLimit() {
        int i = this.describeView.getText().toString().length();
        this.lengthLimitTextView.setText(i + "/500");
    }

    public boolean isMobile(String paramString) {
        if (TextUtils.isEmpty(paramString)) {
            return false;
        }
        return paramString.matches("[1][34578]\\d{9}");
    }

    protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent) {
        super.onActivityResult(paramInt1, paramInt2, paramIntent);
        if ((paramInt1 == 233) && (paramInt2 == -1)) {
            List<Uri> localObject = Matisse.obtainResult(paramIntent);
            if (((List) localObject).size() == 1) {
                this.pickerView.addData(new ImageBean(getRealFilePath(this, (Uri) ((List) localObject).get(0))));
            }else {
                ArrayList arrayList = new ArrayList();
                Iterator<Uri> iterator = localObject.iterator();
                while (((Iterator) localObject).hasNext()) {
                    arrayList.add(new ImageBean(getRealFilePath(this, (Uri) ((Iterator) localObject).next())));
                }
                this.pickerView.addData(arrayList);
            }
        } else {
            return;
        }

    }

    public void onClick(View paramView) {
        switch (paramView.getId()) {

            case R.id.return_goods:
                backToDefaultStyle();
                this.return_goods.setBackgroundResource(R.drawable.btn_aftermarket_green);
                this.return_goods.setTextColor(getResources().getColor(R.color.checkGreenColor));
                this.serviceType = 0;
                break;
            case R.id.change_goods:
                backToDefaultStyle();
                this.change_goods.setBackgroundResource(R.drawable.btn_aftermarket_green);
                this.change_goods.setTextColor(getResources().getColor(R.color.checkGreenColor));
                this.serviceType = 1;
                break;
            default:
                backToDefaultStyle();
                this.other.setBackgroundResource(R.drawable.btn_aftermarket_green);
                this.other.setTextColor(getResources().getColor(R.color.checkGreenColor));
                this.serviceType = 2;
                break;
        }

    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_after_sale_apply);
        this.orderId = getIntent().getIntExtra("orderId", -1);
        this.intentName = getIntent().getStringExtra("name");
        this.intentPhone = getIntent().getStringExtra("phone");
        ButterKnife.bind(this);
        handleImgPicker();
    }

    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if ((paramInt == 4) && (paramKeyEvent.getRepeatCount() == 0)) {
            if (this.clearHistorySearhPopu != null) {
                this.clearHistorySearhPopu.dismiss();
                Intent intent = new Intent(this, OrderDetailActivity.class);
                intent.putExtra("netCode", this.netCode);
                setResult(0, intent);
                Tools.finishAll();
            }else {
                Intent intent = new Intent(this, OrderDetailActivity.class);
                intent.putExtra("netCode", this.netCode);
                setResult(0, intent);
                finish();
            }
            return true;

        }
        return super.onKeyDown(paramInt, paramKeyEvent);
    }

    protected void onResume() {
        super.onResume();
        this.userId = getSharedPreferences("JLAppData", 0).getInt("userId", -1);
        buildProgressDialog();
        init();
    }

    public void setAdapter() {
        CustomLanearLayoutManager localCustomLanearLayoutManager = new CustomLanearLayoutManager(this);
        localCustomLanearLayoutManager.setScrollEnabled(false);
        this.recyclerView.setLayoutManager(localCustomLanearLayoutManager);
        this.goodsListAdapter = new AfterSaleApplyGoodsListAdapter(R.layout.after_sale_detail_goods_list_item, this.orderDetailsBeanList);
        this.recyclerView.setAdapter(this.goodsListAdapter);
    }
}


/* Location:              /Users/davidzhang/Documents/decompileApk/jlfood-dex2jar.jar!/com/jl/jlapp/mvp/activity/AfterSaleApplyActivity.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */